package wapi

import (
	"ims/app/wechat/apptype"
	"time"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gcache"
)

func GetAccessToken(req apptype.Wechat) (interface{}, error) {

	access_token, _ := gcache.Get("access_token")

	if access_token == nil {
		if client, err := g.Client().Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
			req.Appid + "&secret=" + req.AppSecret); err != nil {
			return nil, nil
		} else {
			defer client.Close()
			access_token = client.ReadAllString()
			gcache.SetIfNotExist("access_token", access_token, 6000*time.Second)

			return access_token, nil
		}
	} else {
		return access_token, nil
	}

}
